Rubyにおける信頼性は偶然の産物ではなく、 体系的な専門分野 「早期にテストし、頻繁にテストする」という哲学に基づいています。機能コードと並行してユニットテストを書くことで、デバッグを不快な 考古学的調査 作業から、正確かつリアルタイムでの論理検証へと変化させます。
1. ユニットテストのパラダイム
以下の Test::Unit フレームワークを使用して、ロジックを Test::Unit::TestCaseというクラスで囲みます。プレフィックスが test_ であるメソッドは、個々のコード単位が試され、確認される独立した実験室として機能します。
2. アサーションの仕組み
アサーションはコードの論理ゲートです。 assert_equal(expected, actual) 意図した値と実際の結果を比較します。一致しない場合、テストは失敗し、修復が必要な正確な行を明確に示してくれます。
3. スケーラビリティを考慮した命名規則
一貫性が重要です。個別のテストファイルでは tc_ (テストケース)プレフィックスを使用しますが、コレクションやスイートには ts_ (テストスイート)プレフィックスを使用し、コードベースが成長してもナビゲーションしやすいようにします。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
In the
Test::Unit framework, which naming convention is required for a method to be automatically executed as a test?The method must end with
_test.The method must be named
verify_logic.The method must start with
test_.The method must be inside a
setup block.✅ Correct!
Correct! Ruby's test runner looks for methods starting with 'test_' to identify test cases.❌ Incorrect
The test runner specifically identifies methods starting with the prefix 'test_'.QUESTION 2
What is the primary difference between a file named
tc_orders.rb and ts_app.rb?tc_ is for C++ code; ts_ is for Ruby.tc_ represents an individual Test Case; ts_ represents a Test Suite (collection).tc_ is used for local tests; ts_ is for server tests.There is no functional difference; it is purely aesthetic.
✅ Correct!
Exactly. 'tc' stands for Test Case, while 'ts' stands for Test Suite, which usually requires multiple test cases.❌ Incorrect
The prefix 'tc' is for individual files containing test cases, while 'ts' is for suites that group those files together.QUESTION 3
Why does
assert_equal take two arguments?To compare the 'expected' value with the 'actual' result.
To define the minimum and maximum execution time.
To specify the class name and the method name.
To allocate memory for the test results.
✅ Correct!
Yes. It uses both to provide detailed error messages (e.g., 'Expected A, but got B').❌ Incorrect
Assertions compare your intended outcome (expected) against the code's output (actual).QUESTION 4
What happens if a test fails in the middle of a suite?
The entire computer restarts.
Ruby skips the rest of the file and deletes the log.
The test runner reports the failure and continues to the next test method.
The Ruby interpreter enters an infinite loop.
✅ Correct!
Test runners are designed to report failures while still attempting to run all other tests for maximum visibility.❌ Incorrect
Failures are reported as they happen, but the runner continues to execute remaining tests.QUESTION 5
Which Ruby library must be required to use
Test::Unit::TestCase?require 'debug'require 'test/unit'require 'benchmark'require 'roman'✅ Correct!
Correct. test/unit is the standard library for unit testing in the Pragmatic Programmer's era of Ruby.❌ Incorrect
You must require 'test/unit' to access the TestCase class and assertion methods.Case Study: The 'Archaeology' of Fred's Bug
Applying Unit Testing Principles to Legacy Code
Fred is working on a complex financial system. He spends three days writing a large feature, only to find a bug on day four. He now has to search through 2,000 lines of code to find the error, a process he calls 'code archaeology.'
Q
1. If Fred had unit tested his code as he wrote it, what two specific benefits would he have gained regarding bug detection?
Solution:
First, the unit test would have caught the logic error immediately while the implementation was fresh in his mind. Second, since the test would have targeted only the handful of lines he had just written, he would have identified the bug instantly without having to search through the entire 2,000-line codebase.
First, the unit test would have caught the logic error immediately while the implementation was fresh in his mind. Second, since the test would have targeted only the handful of lines he had just written, he would have identified the bug instantly without having to search through the entire 2,000-line codebase.
Q
2. In the Roman numeral example provided, the second assertion failed. What does the error message tell the developer about the code's behavior?
Solution:
The error message identifies that the assertion expected 'ix' but the code actually returned 'iiiiiiiii'. This reveals that the logic is currently only capable of handling unit increments ('i') and fails to implement the subtraction or grouping logic required for symbols like 'x' or 'v'.
The error message identifies that the assertion expected 'ix' but the code actually returned 'iiiiiiiii'. This reveals that the logic is currently only capable of handling unit increments ('i') and fails to implement the subtraction or grouping logic required for symbols like 'x' or 'v'.